package com.tankgame.models;

import com.tankgame.utils.CollisionManager;
import com.tankgame.utils.Constants;
import com.tankgame.utils.Directions;

import java.awt.*;
import java.util.Iterator;
import java.util.Vector;


public abstract class BaseTank {
    protected final Vector<Missile> missiles = new Vector<>();
    protected int x;
    protected int y;
//    protected boolean missileIsAlive = true;
    protected boolean isAlive = true;
    protected Color color;
    protected Directions direction = Directions.UP;
    protected int speed;
    protected CollisionManager collisionManager = CollisionManager.getInstance();
    protected int id;

    private long lastFireTime = 0; //限制开火速度计时

    public BaseTank(int x, int y, Color color) {
        this.x = x;
        this.y = y;
        this.color = color;
        collisionManager.addTank(this);
//        this.id = id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void fire() {
        //限制开火时间
        long currentTime = System.currentTimeMillis();
        if (currentTime - lastFireTime >= Constants.TANK_FIRE_SPEED) { // 500ms firing rate limit
            Missile missile = new Missile(x, y, color, direction, Constants.MISSILE_SPEED);
            // 设置子弹初始位置
            switch (direction) {
                case UP:
                    missile.setY(missile.getY() - Constants.TANK_HEIGHT / 2);
                    break;
                case DOWN:
                    missile.setY(missile.getY() + Constants.TANK_HEIGHT / 2);
                    break;
                case LEFT:
                    missile.setX(missile.getX() - Constants.TANK_HEIGHT / 2);
                    break;
                case RIGHT:
                    missile.setX(missile.getX() + Constants.TANK_HEIGHT / 2);
                    break;
            }
            missiles.add(missile);
            new Thread(missile).start();
            lastFireTime = currentTime; // Update the last fire time
//            System.out.println(missiles.size());
        }

    }


    public void move() {
//        System.out.println("我移动了");
        final int MARGIN = Math.max(Constants.TANK_HEIGHT, Constants.TANK_WIDTH) / 2;
        final int BARREL_LENGTH = Constants.TANK_BARREL_HEIGHT;

        int newX = x + direction.getDx() * speed;
        int newY = y + direction.getDy() * speed;
//        System.out.println(x + " 新x:" + newX + " y:" + y + " 新y:" + newY);

        // 边界检测
        switch (direction) {
            case UP:
                if (newY - MARGIN - BARREL_LENGTH < 0) return;
                break;
            case DOWN:
                if (newY + MARGIN + BARREL_LENGTH > Constants.GAME_PANEL_HEIGHT) return;
                break;
            case LEFT:
                if (newX - MARGIN - BARREL_LENGTH < 0) return;
                break;
            case RIGHT:
                if (newX + MARGIN + BARREL_LENGTH > Constants.GAME_PANEL_WIDTH) return;
                break;
        }

        // 重叠检测
//        System.out.println(collisionManager.canMove(this, newX, newY));
        if (collisionManager.canMove(this, newX, newY)) {
            // 边界检测
            x = Math.max(MARGIN, Math.min(newX, Constants.GAME_PANEL_WIDTH - MARGIN));
//            System.out.println("边缘检测 x:" + x);
            y = Math.max(MARGIN, Math.min(newY, Constants.GAME_PANEL_HEIGHT - MARGIN));
//            System.out.println("边缘检测 y:" + y);
        }
    }

    public void draw(Graphics2D g) {
        // 创建 Graphics2D 的副本
        Graphics2D g2d = (Graphics2D) g.create();
        try {
            // 移动到坦克的位置
            g2d.translate(x, y);

            // 根据方向进行旋转
            switch (direction) {
                case UP:
                    g2d.rotate(Math.toRadians(0));
                    break;
                case DOWN:
                    g2d.rotate(Math.toRadians(180));
                    break;
                case LEFT:
                    g2d.rotate(Math.toRadians(-90));
                    break;
                case RIGHT:
                    g2d.rotate(Math.toRadians(90));
                    break;
            }

            // 绘制坦克
            drawTracks(g2d);
            drawBody(g2d);
            drawTurret(g2d);
            drawBarrel(g2d);

            g.setColor(Color.black);
            g.drawString("id" + String.valueOf(id), x - 15, y - 20);
        } finally {
            // 释放副本
            g2d.dispose();
        }
    }

    private void drawTracks(Graphics2D g) {
        g.setColor(Constants.TANK_OTHER_PARTS_COLOR);
        // 左履带
        g.fillRoundRect(-Constants.TANK_WIDTH / 2, -Constants.TANK_TRACK_HEIGHT / 2,
                Constants.TANK_TRACK_WIDTH, Constants.TANK_TRACK_HEIGHT, Constants.TANK_OTHER_PARTS_ARC, Constants.TANK_OTHER_PARTS_ARC);
        // 右履带
        g.fillRoundRect(Constants.TANK_WIDTH / 2 - Constants.TANK_TRACK_WIDTH, -Constants.TANK_TRACK_HEIGHT / 2,
                Constants.TANK_TRACK_WIDTH, Constants.TANK_TRACK_HEIGHT, Constants.TANK_OTHER_PARTS_ARC, Constants.TANK_OTHER_PARTS_ARC);
    }

    private void drawBody(Graphics2D g) {
        g.setColor(color);
        g.fillRect(-Constants.TANK_BODY_WIDTH / 2, -Constants.TANK_BODY_HEIGHT / 2,
                Constants.TANK_BODY_WIDTH, Constants.TANK_BODY_HEIGHT);
    }

    private void drawTurret(Graphics2D g) {
        g.setColor(Constants.TANK_OTHER_PARTS_COLOR);
        g.fillOval(-Constants.TANK_TURRET_DIAMETER / 2, -Constants.TANK_TURRET_DIAMETER / 2,
                Constants.TANK_TURRET_DIAMETER, Constants.TANK_TURRET_DIAMETER);
    }

    private void drawBarrel(Graphics2D g) {
        g.setColor(Constants.TANK_OTHER_PARTS_COLOR);
        g.fillRoundRect(-Constants.TANK_BARREL_WIDTH / 2, -Constants.TANK_BARREL_HEIGHT,
                Constants.TANK_BARREL_WIDTH, Constants.TANK_BARREL_HEIGHT, Constants.TANK_OTHER_PARTS_ARC, Constants.TANK_OTHER_PARTS_ARC);
    }

    public void drawMissiles(Graphics2D g, Vector<BaseTank> enemyTanks) {
        for (Iterator<Missile> it = missiles.iterator(); it.hasNext(); ) {
            Missile missile = it.next();
            if (missile != null && missile.isAlive()) {
                missile.draw(g);
                // 检测子弹是否击中其他坦克
                for (Iterator<BaseTank> tankIt = enemyTanks.iterator(); tankIt.hasNext(); ) {
                    BaseTank tank = tankIt.next();
                    if (tank != this && missile.collidesWith(tank)) {
                        tank.setAlive(false);
                        missile.setAlive(false);
                        tankIt.remove(); // 使用 Iterator 的 remove 方法
                    }
                }
            } else it.remove();
        }
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public boolean isAlive() {
        return isAlive;
    }

    public void setAlive(boolean alive) {
        isAlive = alive;
    }

    public Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        this.color = color;
    }

    public Directions getDirection() {
        return direction;
    }

    public void setDirection(Directions direction) {
        this.direction = direction;
    }

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public Vector<Missile> getMissiles() {
        return missiles;
    }

    public int getId() {
        return id;
    }

    @Override
    public String toString() {
        return "BaseTank{" +
                "missiles=" + missiles +
                ", x=" + x +
                ", y=" + y +
                ", isAlive=" + isAlive +
                ", color=" + color +
                ", direction=" + direction +
                ", speed=" + speed +
                ", collisionManager=" + collisionManager +
                ", id=" + id +
                ", lastFireTime=" + lastFireTime +
                '}';
    }
}
